import { reqgetbanner, reqgetcoupon } from "../../http/api";


const initState = {
    bannerlist: [],
    couponlist: [],
    n: 0
}

const types = {
    CHANGE_BANNER: "MINE_CHANGE_BANNER",
    CHANGE_CONPONLIST: "MINE_CHANGE_CONPONLIST",
    CHANGE_N: "MINE_CHANGE_N"
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_BANNER:
            return {
                ...state,
                bannerlist: [...action.list]
            }
        case types.CHANGE_CONPONLIST:
            return {
                ...state,
                couponlist: [...action.coupon]
            }
        case types.CHANGE_N:
            return {
                ...state,
                n: action.n
            }
        default:
            return state
    }
}

export const actions = {
    changeBanner: list => ({ type: types.CHANGE_BANNER, list }),
    reqgetbanner: () => (dispatch, getState) => {
        reqgetbanner().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeBanner(res.data.list))
            }
        })
    },
    changeConponlist: coupon => ({ type: types.CHANGE_CONPONLIST, coupon }),
    reqgetcoupon: () => (dispatch, getState) => {
        reqgetcoupon().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeConponlist(res.data.list))
            }
        })
    },
    changeN:n => ({type:types.CHANGE_N,n}),
}

export const minebanner = state => state.Mine.bannerlist;
export const mineconpon = state => state.Mine.couponlist;
export const changeN = state => state.Mine.n;

export default reducer;